package com.videoweb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.videoweb.model.ChunkInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ChunkInfoMapper extends BaseMapper<ChunkInfo> {
    
    /**
     * 根据文件哈希查询所有分片信息
     */
    @Select("SELECT * FROM chunk_info WHERE file_hash = #{fileHash} ORDER BY chunk_index")
    List<ChunkInfo> findByFileHash(@Param("fileHash") String fileHash);
    
    /**
     * 查询指定文件的已上传分片索引列表
     */
    @Select("SELECT chunk_index FROM chunk_info WHERE file_hash = #{fileHash} ORDER BY chunk_index")
    List<Integer> findUploadedChunkIndexes(@Param("fileHash") String fileHash);
    
    /**
     * 检查指定分片是否存在
     */
    @Select("SELECT COUNT(*) FROM chunk_info WHERE file_hash = #{fileHash} AND chunk_index = #{chunkIndex}")
    int countByFileHashAndChunkIndex(@Param("fileHash") String fileHash, @Param("chunkIndex") Integer chunkIndex);
    
    /**
     * 根据文件哈希和分片索引查询分片信息
     */
    @Select("SELECT * FROM chunk_info WHERE file_hash = #{fileHash} AND chunk_index = #{chunkIndex}")
    ChunkInfo findByFileHashAndChunkIndex(@Param("fileHash") String fileHash, @Param("chunkIndex") Integer chunkIndex);
    
    /**
     * 删除指定文件的所有分片信息
     */
    @Delete("DELETE FROM chunk_info WHERE file_hash = #{fileHash}")
    int deleteByFileHash(@Param("fileHash") String fileHash);
    
    /**
     * 获取文件的总分片数量
     */
    @Select("SELECT COUNT(*) FROM chunk_info WHERE file_hash = #{fileHash}")
    int countChunksByFileHash(@Param("fileHash") String fileHash);
} 